<?php
/* -------------------------------------------------------------------------------------
* 	ID:						$Id: products_attributes.php 100 2013-09-21 12:06:00Z phone.mueller@googlemail.com $
* 	Letzter Stand:			$Revision: 100 $
* 	zuletzt geaendert von:	$Author: siekiera $
* 	Datum:					$Date: 2013-09-21 12:06:00 +0000 (Sat, 21 Sep 2013) $
*
* 	SEO:mercari by Siekiera Media
* 	http://www.seo-mercari.de
*
* 	Copyright (c) since 2011 SEO:mercari
* --------------------------------------------------------------------------------------
* 	based on:
* 	(c) 2000-2001 The Exchange Project  (earlier name of osCommerce)
* 	(c) 2002-2003 osCommerce - www.oscommerce.com
* 	(c) 2003     nextcommerce - www.nextcommerce.org
* 	(c) 2005     xt:Commerce - www.xt-commerce.com
*
* 	Released under the GNU General Public License
* ----------------------------------------------------------------------------------- */

require('includes/application_top.php');
require(DIR_FS_INC.'inc.get_hex_image.php');
$languages = get_languages();

if(isset($_GET['per_site']))
	$_SESSION['ol'] = $_GET['per_site'];
elseif(isset($_SESSION['ol']))
	$_SESSION['ol'] = $_SESSION['ol'];
else
	$_SESSION['ol'] = 25;

$option_order_by = (isset($_GET['option_order_by'])?$_GET['option_order_by']:'products_options_id');
$option_page = (isset($_GET['option_page']) ? $_GET['option_page'] : '1');
$option_search = (isset($_GET['searchoption']) ? $_GET['searchoption'] : '');
$opt_add = 'option_order_by='.$option_order_by.'&option_page='.$option_page.(!empty($option_search) ? '&searchoption='.$option_search : '');

$value_order_by = (isset($_GET['value_order_by']) ? $_GET['value_order_by'] : 'products_options_values_id');
$value_page = (isset($_GET['value_page']) ? $_GET['value_page'] : '1');
$value_search = (isset($_GET['search_value']) ? $_GET['search_value'] : '');
$val_add = 'value_order_by='.$value_order_by.'&value_page='.$value_page.(!empty($search_value) ? '&search_value='.$search_value : '');

if(isset($_GET['action']))
    include(DIR_WS_MODULES.'attribute/actions.php');

switch($_GET['do']) {
	case 'new_option':
		include(DIR_WS_MODULES.'attribute/new_option.php');
		break;
	
	case 'edit_option':
		include(DIR_WS_MODULES.'attribute/edit_option.php');
		break;
		
	case 'del_option':
		include(DIR_WS_MODULES.'attribute/delete_option.php');
		break;
	
	case 'new_value':
		include(DIR_WS_MODULES.'attribute/new_value.php');
		echo '	<script type="text/javascript" src="includes/javascript/miniColor/jquery.miniColors.js"></script>
				<script type="text/javascript">$(".multiple").miniColors();</script>';
		break;
	
	case 'edit_value':
		include(DIR_WS_MODULES.'attribute/edit_value.php');
		echo '	<script type="text/javascript" src="includes/javascript/miniColor/jquery.miniColors.js"></script>
				<script type="text/javascript">$(".multiple").miniColors();</script>';
		break;
		
	case 'del_value':
		include(DIR_WS_MODULES.'attribute/delete_value.php');
		break;
	
	default:
		require(DIR_WS_INCLUDES.'metatag.php'); ?>
		<link type="text/css" rel="stylesheet" href="includes/javascript/miniColor/jquery.miniColors.css" />
		<script type="text/javascript">
			function go_option() {
				if (document.option_order_by.selected.options[document.option_order_by.selected.selectedIndex].value != "none") {
					location = "<?php echo href_link(FILENAME_PRODUCTS_ATTRIBUTES, getMenuID('products_attributes').'&option_page='.($_GET['option_page'] ? $_GET['option_page'] : 1)); ?>&option_order_by="+document.option_order_by.selected.options[document.option_order_by.selected.selectedIndex].value;
				}
			}
			function go_value() {
				if (document.value_order_by.selected.options[document.value_order_by.selected.selectedIndex].value != "none") {
					location = "<?php echo href_link(FILENAME_PRODUCTS_ATTRIBUTES, getMenuID('products_attributes').'&value_page='.($_GET['option_page'] ? $_GET['option_page'] : 1).'&value_tab=1'); ?>&value_order_by="+document.value_order_by.selected.options[document.value_order_by.selected.selectedIndex].value;
				}
			}
			$(function() {
				<?php
					if(isset($_GET['value_tab']) && $_GET['value_tab'])
						$sel = ',selected: 1';
				?>
				$('#tabs').tabs({fx:{opacity:'toggle'}<?php echo $sel ?>});
			});
		</script>
		</head>
		<body>
		<?php require(DIR_WS_INCLUDES.'header.php'); ?>
		<div id="wrapper">
			<table class="outerTable" cellpadding="0" cellspacing="0">
				<tr>
					<td class="columnLeft2" width="<?php echo BOX_WIDTH; ?>" valign="top">
						<?php require(DIR_WS_INCLUDES.'column_left.php'); ?>
					</td>
					<td class="boxCenter" valign="top">
						<table width="100%" cellspacing="0" cellpadding="0">
							<tr>
								<td width="100%">
									<div class="ar">
										Listenlänge:
										<form action="<?php echo href_link('products_attributes.php'); ?>" name="per_site_form" method="GET">
											<input type="hidden" name="menu" value="<?php echo substr(getmenuID('products_attributes'), -1, 1) ?>" />
											<select name="per_site" onchange="this.form.submit()" style="width:auto">
												<option value="25"<?php echo ($_SESSION['ol'] == 25 ? ' selected="selected"':''); ?>>25</option>
												<option value="50"<?php echo ($_SESSION['ol'] == 50 ? ' selected="selected"':''); ?>>50</option>
												<option value="100"<?php echo ($_SESSION['ol'] == 100 ? ' selected="selected"':''); ?>>100</option>
											</select>
										</form>
									</div>
									<div id="tabs">
										<ul>
											<li><a href="#option" name="option"><span><?php echo HEADING_TITLE_OPT ?></span></a></li>
											<li><a href="#value" name="value"><span><?php echo HEADING_TITLE_ATRIB ?></span></a></li>
										</ul>
										<div id="option">
											<table width="100%">
												<tr>
													<td align="left">
														<button class="button" onclick="dialogBox('?do=new_option', 600, 300, 'neues Attribut','true')">
															neues Attribut
														</button>
													</td>
													<td align="right">
														Sortieren nach:
														<form name="option_order_by" action="<?php echo FILENAME_PRODUCTS_ATTRIBUTES; ?>">
															<select name="selected" onchange="go_option()">
																<option value="products_options_id"<?php if ($_GET['option_order_by'] == 'products_options_id') { echo ' selected'; } ?>> <?php echo TEXT_OPTION_ID; ?></option>
																<option value="products_options_name"<?php if ($_GET['option_order_by'] == 'products_options_name') { echo ' selected'; } ?>> <?php echo TEXT_OPTION_NAME; ?></option>
															</select>
														</form> 
														<form name="search" action="<?php echo FILENAME_PRODUCTS_ATTRIBUTES; ?>" method="GET">
															<?php echo TEXT_SEARCH; ?>
															<input type="text" name="searchoption" size="20" value="<?php echo $option_search; ?>" />
														</form>
													</td>
												</tr>
											</table>
											<table width="100%">
													<tr>
														<td colspan="3">
														<?php	
															$per_page = $_SESSION['ol'];
															$options = "SELECT
																			*
																		FROM
																			".TABLE_PRODUCTS_OPTIONS."
																		WHERE
																			language_id = '".$_SESSION['languages_id']."'
																		".(!empty($option_search) ? " AND products_options_name LIKE '%".$option_search."%' " : '')."
																		ORDER BY
																			".$option_order_by;

															$prev_option_page = $option_page - 1;
															$next_option_page = $option_page + 1;

															$option_query = $db->db_query($options);

															$option_page_start = ($per_page * $option_page) - $per_page;
															$num_rows = $option_query->_numOfRows;

															if ($num_rows <= $per_page)
																$num_pages = 1;
															else if (($num_rows % $per_page) == 0)
																$num_pages = ($num_rows / $per_page);
															else
																$num_pages = ($num_rows / $per_page) + 1;

															$num_pages = (int) $num_pages;

															$options = $options." LIMIT $option_page_start, $per_page";
															if($num_pages > $_SESSION['ol']) {
																// zurück
																if ($prev_option_page)
																	echo '<a href="'.href_link(FILENAME_PRODUCTS_ATTRIBUTES, 'option_page='.$prev_option_page.'&searchoption='.$_GET['searchoption'].'#option').'"> &lt;&lt; </a> | ';

																for ($i = 1; $i <= $num_pages; $i++) {
																	if ($i != $option_page)
																		echo '<a href="'.href_link(FILENAME_PRODUCTS_ATTRIBUTES, 'option_page='.$i.'&searchoption='.$_GET['searchoption'].'#option').'">'.$i.'</a> | ';
																	else
																		echo '<b><span style="color:#b20000">'.$i.'</span></b> | ';
																}

																// vorwärts
																if ($option_page != $num_pages)
																	echo '<a href="'.href_link(FILENAME_PRODUCTS_ATTRIBUTES, 'option_page='.$next_option_page.'&searchoption='.$_GET['searchoption'].'#option').'"> &gt;&gt; </a>';
															} ?>
														</td>
													</tr>
													<?php
													$next_id = 1;
													$options = $db->db_query($options);

													if($options->_numOfRows) { ?>
														<tr class="dataTableHeadingRow">
															<td class="dataTableHeadingContent" width="1"><?php echo TABLE_HEADING_ID; ?></td>
															<td class="dataTableHeadingContent"><?php echo TABLE_HEADING_OPT_NAME; ?></td>
															<td class="dataTableHeadingContent" width="1"><?php echo TABLE_HEADING_ACTION; ?></td>
														</tr>
														<?php
														while (!$options->EOF) { $rows++; ?>
															<tr class="<?php echo (floor($rows/2) == ($rows/2) ? 'attributes-even' : 'attributes-odd'); ?>">
																<td align="center" width="1"><?php echo $options->fields['products_options_id']; ?></td>
																<td>
																	<?php
																		if($options->fields['products_options_image'] !='')
																			echo '<img class="opt_val_img" src="../images/product_options/'.$options->fields['products_options_image'].'" alt="" align="left" style="margin: 0 10px 10px 0" />';
																		echo $options->fields['products_options_name'];
																	?>
																</td>
																<td align="center">
																	<nobr>
																	<button class="button" onclick="dialogBox('?do=edit_option&option_id=<?php echo $options->fields['products_options_id'].'&'.$opt_add ?>', 600, 300, 'Attribut bearbeiten','true')">
																		<?php echo BUTTON_EDIT ?>
																	</button> 
																	<button class="button" onclick="dialogBox('?do=del_option&option_id=<?php echo $options->fields['products_options_id'].'&'.$opt_add ?>', 600, 300, 'Attribut löschen')">
																		<?php echo BUTTON_DELETE ?>
																	</button>
																	</nobr>
																</td>
															</tr>
															<?php
															$options->MoveNext();
														}
													} else
														echo '<tr><td colspan="3" align="center"><em>keine Attribute gefunden</em></td></tr>';
												?>
											</table>
										</div>
										<!-- value //-->
										<div id="value">
											<table width="100%" cellpadding="2">
												<tr>
													<td colspan="2">
														<?php if($options->_numOfRows) { ?>
														<button class="button" onclick="dialogBox('?do=new_value&<?php echo $val_add ?>', 650, 380, 'neuer Attributwert','true')">
															neuer Attributwert
														</button>
														<?php } ?>
													</td>
													<td colspan="2" align="right">
														<nobr>
															Sortieren nach:
															<form name="value_order_by" action="<?php echo href_link(FILENAME_PRODUCTS_ATTRIBUTES, $val_add.'#value'); ?>">
																<select name="selected" onchange="go_value()" style="width:auto">
																	<option value="products_options_values_id"<?php if ($_GET['value_order_by'] == 'products_options_values_id') { echo ' selected'; } ?>>Value ID</option>
																	<option value="products_options_values_name"<?php if ($_GET['value_order_by'] == 'products_options_values_name') { echo ' selected'; } ?>>Value Name</option>
																</select>
															</form> 
															<form name="search" action="<?php echo href_link(FILENAME_PRODUCTS_ATTRIBUTES, $val_add.'&value_page=1'); ?>" method="GET">
																<?php echo TEXT_SEARCH; ?> <input type="text" name="search_value" size="20" value="<?php echo $value_search;?>">
															</form>
														</nobr>
													</td>
												</tr>
												<tr>
												<td colspan="4" align="center"><?php
													$per_page = $_SESSION['ol'];
													if (!empty($value_search)) {
														$values = "SELECT DISTINCT
																		pov.products_options_values_id,
																		pov.products_options_values_name,
																		pov2po.products_options_id
																	FROM
																		".TABLE_PRODUCTS_OPTIONS." po,
																		".TABLE_PRODUCTS_OPTIONS_VALUES." pov
																	LEFT JOIN
																		".TABLE_PRODUCTS_OPTIONS_VALUES_TO_PRODUCTS_OPTIONS." pov2po
																			ON pov.products_options_values_id = pov2po.products_options_values_id
																	WHERE
																		pov.language_id = '".$_SESSION['languages_id']."'
																	AND
																		pov2po.products_options_id = po.products_options_id
																	AND (po.products_options_name
																			LIKE
																				'%".$value_search."%'
																			OR
																				pov.products_options_values_name
																			LIKE
																				'%".$value_search."%')
																	ORDER BY
																		pov.".$value_order_by;
													} else {
														$values = "SELECT
																		pov.products_options_values_id,
																		pov.products_options_values_name,
																		pov2po.products_options_id
																	FROM
																		".TABLE_PRODUCTS_OPTIONS_VALUES." pov
																	LEFT JOIN
																		".TABLE_PRODUCTS_OPTIONS_VALUES_TO_PRODUCTS_OPTIONS." pov2po
																	ON
																		pov.products_options_values_id = pov2po.products_options_values_id
																	WHERE
																		pov.language_id = '".$_SESSION['languages_id']."'
																	ORDER BY
																		pov.".$value_order_by;
													}
													$prev_value_page = $value_page-1;
													$next_value_page = $value_page+1;

													$value_query = $db->db_query($values);

													$value_page_start = ($per_page * $value_page) - $per_page;
													$num_rows = $value_query->_numOfRows;

													if ($num_rows <= $per_page)
														$num_pages = 1;
													else if (($num_rows % $per_page) == 0)
														$num_pages = ($num_rows / $per_page);
													else
														$num_pages = ($num_rows / $per_page) + 1;

													$num_pages = (int)$num_pages;

													$values = $values." LIMIT $value_page_start, $per_page";
													if($num_rows > $_SESSION['ol']) {
														// zurück
														if ($prev_value_page)
														echo '<a href="'.href_link(FILENAME_PRODUCTS_ATTRIBUTES, 'option_order_by='.$option_order_by.'&value_page='.$prev_value_page.'&search_optionsname='.$_GET['search_optionsname'].'&value_tab=1').'"> &laquo;</a> | ';

														for ($i = 1; $i <= $num_pages; $i++) {
															if ($i != $value_page)
																echo '<a href="'.href_link(FILENAME_PRODUCTS_ATTRIBUTES, 'value_order_by='.$value_order_by.'&value_page='.$i.(!empty($search_value) ? '&search_value='.$search_value:'').'&value_tab=1').'">'.$i.'</a> | ';
															else
																echo '<b><span style="color:#b20000">'.$i.'</span></b> | ';
														}

														// vorwärts
														if ($value_page != $num_pages)
															echo '<a href="'.href_link(FILENAME_PRODUCTS_ATTRIBUTES, 'value_order_by='.$value_order_by.'&value_page='.$next_value_page.(!empty($search_value) ? '&search_value='.$search_value:'')).'"> &raquo;</a> ';
													}
													?>
												</td>
												</tr>
												<?php
												$next_id = 1;
												$values = $db->db_query($values);
												if($values->_numOfRows) { ?>
													<tr class="dataTableHeadingRow">
														<td class="dataTableHeadingContent" width="1"><?php echo TABLE_HEADING_ID; ?></td>
														<td class="dataTableHeadingContent"><?php echo TABLE_HEADING_OPT_NAME; ?></td>
														<td class="dataTableHeadingContent"><?php echo TABLE_HEADING_OPT_VALUE; ?></td>
														<td class="dataTableHeadingContent" width="1"><?php echo TABLE_HEADING_ACTION; ?></td>
													</tr>
													<?php while (!$values->EOF) {
														$options_name = options_name($values->fields['products_options_id']);
														$values_name = $values->fields['products_options_values_name'];
														$rows++;
														?>
															<tr class="<?php echo (floor($rows/2) == ($rows/2) ? 'attributes-even' : 'attributes-odd'); ?>">
															<td width="1">
																<?php echo $values->fields["products_options_values_id"]; ?>
															</td>
															<td>
																<?php echo $options_name; ?>
															</td>
															<td>
																<?php echo (get_hex_image($values->fields["products_options_values_id"]) ? get_hex_image($values->fields["products_options_values_id"]).' ' : '').$values_name; ?>
															</td>
															<td align="center"><nobr>
																<button class="button" onclick="dialogBox('?do=edit_value&value_id=<?php echo $values->fields['products_options_values_id'].'&'.$val_add ?>', 650, 380, 'Attributwert bearbeiten','true')">
																	<?php echo BUTTON_EDIT ?>
																</button> 
																<button class="button" onclick="dialogBox('?do=del_value&value_id=<?php echo $values->fields['products_options_values_id'].'&'.$val_add ?>', 600, 300, 'Attributwert löschen')">
																	<?php echo BUTTON_DELETE ?>
																</button>
																</nobr>
															</td>
														</tr>
														<?php
														$values->MoveNext();
													}

													if($num_rows > $_SESSION['ol']) {
														echo '<tr><td colspan="4" align="center">';
														// zurück
														if ($prev_value_page)
														echo '<a href="'.href_link(FILENAME_PRODUCTS_ATTRIBUTES, 'option_order_by='.$option_order_by.'&value_page='.$prev_value_page.'&search_optionsname='.$_GET['search_optionsname'].'&value_tab=1').'"> &laquo;</a> | ';

														for ($i = 1; $i <= $num_pages; $i++) {
															if ($i != $value_page)
																echo '<a href="'.href_link(FILENAME_PRODUCTS_ATTRIBUTES, 'value_order_by='.$value_order_by.'&value_page='.$i.(!empty($search_value) ? '&search_value='.$search_value:'').'&value_tab=1').'">'.$i.'</a> | ';
															else
																echo '<b><span style="color:#b20000">'.$i.'</span></b> | ';
														}
														// vorwärts
														if ($value_page != $num_pages)
															echo '<a href="'.href_link(FILENAME_PRODUCTS_ATTRIBUTES, 'value_order_by='.$value_order_by.'&value_page='.$next_value_page.(!empty($search_value) ? '&search_value='.$search_value:'')).'"> &raquo;</a> ';
														echo '</td></tr>';
													}

													if($values->_numOfRows > 15) {
														echo '<tr><td colspan="3">
																<button class="button" onclick="dialogBox(\'?do=new_value&'.$val_add.'\', 650, 380, \'neuer Attributwert\',\'true\')">
																		neuer Attributwert
																	</button> 
																</td></tr>';
													}
												} else
													echo '<tr><td colspan="4" align="center"><em>keine Attributwerte gefunden</em></td></tr>';
												?>
											</table>
										</div>
									</div>
								</td>
							</tr>
						</table>
					</td>
		        </tr>
		    </table>
		</div>
		<script type="text/javascript">
			function dialogBox(url, w, h, title, save) {
				<?php if(isset($_SESSION['customer_id'])) { ?>
					if(save == 'true') {
						var extra_button = {"<?php echo BUTTON_SAVE ?>":function(){$("#dialog_form").submit();},"<?php echo BUTTON_CANCEL ?>": function() {$(this).dialog("close");}};
					} else {var extra_button = '';}
					
		            var dialog = $('<div style="display:none" class="loading"></div>').appendTo('body');
		            
		            dialog.dialog({
		                close: function(event, ui) {dialog.remove();},
		                modal: true,
		                title: title,
		                buttons: extra_button,
		                width: w,
		                height: h
		            });
		            dialog.load(url,{},function (responseText, textStatus, XMLHttpRequest){
		            	dialog.removeClass('loading');
			            $('.ui-dialog-content').css({backgroundImage:'none'});
		            });
		            $('.ui-dialog').css({position:"fixed"});

				<?php } else { ?>
					window.location.href='<?php echo catalog_href_link('login.php', '', 'SSL'); ?>';
				<?php } ?>
				return false;
			}
		</script>
		<?php require(DIR_WS_INCLUDES.'footer.php'); ?>
		</body>
		</html>
		<?php require(DIR_WS_INCLUDES.'application_bottom.php');
		break;	
}